from django.db import models


# Create your models here.
class Userinfo(models.Model):
    username = models.CharField(max_length=30, verbose_name='用户名')
    password = models.CharField(max_length=30, verbose_name='密码')
    email = models.EmailField(verbose_name='邮箱')
    phone = models.CharField(verbose_name='手机号',max_length=11)

    def __str__(self):
        return self.username

    class Meta:
        db_table = 'userinfo'


class Author(models.Model):
    author = models.CharField(max_length=50, verbose_name='作者姓名')
    sex_info = ((1, '男'), (2, '女'))
    sex = models.CharField(max_length=1, choices=sex_info, default=1, verbose_name='性别')
    age = models.CharField(max_length=3, verbose_name='年龄')
    phone = models.CharField(max_length=11, verbose_name='作者电话')
    email = models.EmailField(verbose_name='作者邮箱')

    def __str__(self):
        return self.author

    class Meta:
        db_table = 'author'


class Publisher(models.Model):
    publisher = models.CharField(max_length=100, verbose_name='出版社名称')
    address = models.CharField(max_length=100,null=True, verbose_name='出版社地址',)
    email = models.EmailField(verbose_name='出版社邮箱')
    phone = models.CharField(max_length=11,verbose_name='出版社电话')


    def __str__(self):
        return self.publisher

    class Meta:
        db_table = 'publisher'


class Book(models.Model):
    title = models.CharField(max_length=100, verbose_name='书名')
    date = models.DateField(verbose_name='发行时间')
    author = models.ForeignKey(Author, on_delete=models.CASCADE, verbose_name='作者')
    publisher = models.ForeignKey(Publisher, on_delete=models.CASCADE, verbose_name='出版社')

    def __str__(self):
        return self.title

    class Meta:
        db_table = 'book'
